import 'package:flutter/material.dart';
import 'package:flutter_mode/other/entries.dart';
import 'package:flutter_mode/utils.dart';

class InheritedDemoPage extends StatelessWidget {
  static final GlobalKey<FormState> formKey = new GlobalKey<FormState>();
  static final GlobalKey<FormFieldState<String>> nameKey =
  new GlobalKey<FormFieldState<String>>();
  static final GlobalKey<FormFieldState<String>> birthKey =
  new GlobalKey<FormFieldState<String>>();

  const InheritedDemoPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final appInherited = AppInheritedWidget.of(context);
    final appDataManager = appInherited.appDataManager;
    var user = appDataManager.user;

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('输入登录用户'),
        actions: <Widget>[
          IconButton(
              icon: Icon(Icons.save),
              onPressed: () {
                final form = formKey.currentState!;
                if (form.validate()) {
                  var name = nameKey.currentState!.value;
                  DateTime? birth;

                  if (name == '') {
                    showToast('请输入姓名！');
                    return;
                  }

                  var bir = birthKey.currentState!.value;
                  if (bir?.isNotEmpty == true) {
                    try {
                      birth = DateTime.parse(bir!);
                    } catch (e) {
                      showToast('出生日期格式错误！');
                      return;
                    }
                  } else {
                    birth = null;
                  }

                  if(appDataManager != null) {
                    appDataManager.user = User(name!, birth);
                  }else{
                    showToast('InheritedWidget 使用失败');
                    return;
                  }

                  Navigator.pop(context);
                }
              })
        ],
      ),
      body: Padding(
        padding: EdgeInsets.all(16),
        child: Form(
            key: formKey,
            child: ListView(
              children: <Widget>[
                TextFormField(
                  key: nameKey,
                  style: Theme.of(context).textTheme.headlineSmall,
                  decoration: InputDecoration(hintText: '姓名',),
                  initialValue: user == null? '': user.name,
                ),
                TextFormField(
                  key: birthKey,
                  style: Theme.of(context).textTheme.headlineSmall,
                  decoration: InputDecoration(hintText: '出生日期（选填）'),
                  initialValue: user == null || user.dateOfBirth == null? '': user.dateOfBirth.toString(),
                ),
              ],
            )),
      ),
    );
  }
}

class AppInheritedWidget extends InheritedWidget {
  final AppDataManager appDataManager;

  AppInheritedWidget(this.appDataManager, { Key? key, required Widget child }):super(key:key, child:child);

  static AppInheritedWidget of(BuildContext context){
    return context.dependOnInheritedWidgetOfExactType(aspect: AppInheritedWidget)!;
  }

  @override
  bool updateShouldNotify(AppInheritedWidget oldWidget) {
    // TODO: implement updateShouldNotify
    return true;
  }

}